package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.AduserConstants;
import com.heima.model.admin.dtos.AuthDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.LoginDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import com.heima.user.service.ApUserService;
import com.heima.utils.common.AppJwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Author bo.li
 * @Date 2023/1/29 11:45
 * @Version 1.0
 */
@Service
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApUserService {
    @Autowired
    private ApUserRealnameMapper apUserRealnameMapper;
    @Autowired
    private ApUserMapper apUserMapper;
    @Override
    public ResponseResult login(LoginDto loginDto) {
        //1.登录
        //用户名和密码都不为空的时候表示登录
        if (!StringUtils.isEmpty(loginDto.getPhone()) && !StringUtils.isEmpty(loginDto.getPassword())) {
            //1.1 根据用户名查询数据库  要么有  要么没有
            ApUser apUser = getOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getPhone, loginDto.getPhone()));
            if (apUser == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
            }
            //1.2 拿到salt
            String salt = apUser.getSalt();
            //1.3 传过来的密码+salt   再加密
            String newPassword = DigestUtils.md5DigestAsHex((loginDto.getPassword() + salt).getBytes());
            //1.4 对比1.3步骤生成的密码和数据库查出的密码
            if (!newPassword.equals(apUser.getPassword())) {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
            //1.5 封装响应对象
            Map<String, Object> result = new HashMap<>();
            apUser.setSalt("");
            apUser.setPassword("");
            result.put("user", apUser);
            result.put("token", AppJwtUtil.getToken(apUser.getId().longValue()));

            return ResponseResult.okResult(result);
        } else {
            //2.不登录
            Map<String, Object> result = new HashMap<>();
            result.put("token", AppJwtUtil.getToken(0l));
            return ResponseResult.okResult(result);
        }
    }

    @Override
    public ResponseResult authFail(AuthDto dto) {
        ApUserRealname apUserRealname = apUserRealnameMapper.selectById(dto.getId());
        apUserRealname.setStatus(AduserConstants.STATUS_FAIL);
        apUserRealnameMapper.updateById(apUserRealname);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult authPass(AuthDto dto) {
        ApUserRealname apUserRealname = apUserRealnameMapper.selectById(dto.getId());
        apUserRealname.setStatus(AduserConstants.STATUS_PASS);
        apUserRealnameMapper.updateById(apUserRealname);
        ApUser apUser = apUserMapper.selectById(dto.getId());
        return ResponseResult.okResult(apUser);
    }


}